<!--
 * @Author: zh
 * @Date: 2024-11-28 16:38:36
 * @LastEditors: zh
 * @LastEditTime: 2024-11-28 16:53:50
 * @Description: 
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Print Part of Page</title>
    <style>
        /* 隐藏打印区域 */
        /* @media screen {
            #printArea {
                display: none;
            }
        } */

        /* 显示打印区域 */
        @media print {
            body * {
                visibility: hidden;
            }

            #contentToPrint,
            #contentToPrint * {
                visibility: visible;
            }

            #contentToPrint {
                position: absolute;
                left: 0;
                top: 0;
                width: 100%;
                height: 100%;
            }
        }
    </style>
</head>

<body>
    <h1 id="noPrint">不打印</h1>

    <div id="contentToPrint">
        <h1>这是需要打印的内容</h1>
        <p>这是一段示例文本。</p>
    </div>

    <button onclick="printContent()">打印内容</button>


    <script>
        function printContent() {
            // 获取需要打印的内容
            const content = document.getElementById('contentToPrint').innerHTML;
            // 触发打印
            window.print();
        }
    </script>
</body>

</html>